package com.moli.iov.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.moli.iov.model.SMSResponse;

/**
 * @author fudh
 * @ClassNmme SMSUtil
 * @date 2019/5/21 16:24
 * @Description: 短信发送util
 */
public class SMSUtil {

    /**
     * 短信发送接口
     *
     * @param phoneNumbers 发送目的手机号,支持群发，群发的适合手机号之间需要使用英文逗号分隔
     * @param templeteCode 发送模板code
     * @param sign         签名
     * @param jsonData     短信json数据，如{"code": "123456"}
     * @return
     */
    public static SMSResponse sendMessage(String accessKeyId, String secret, String phoneNumbers,
                                          String templeteCode, String sign, String jsonData) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("TemplateCode", templeteCode);
        request.putQueryParameter("SignName", sign);
        request.putQueryParameter("TemplateParam", jsonData);
        CommonResponse response = client.getCommonResponse(request);
        String retStr = response.getData();
        SMSResponse smsResponse = JSONObject.parseObject(retStr, SMSResponse.class);
        return smsResponse;
    }

}
